<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	/*---------首页---------*/
		public function indexx()
		{
			if(!isset($_SESSION['accounts']))
			{
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('请登录');location.href='".U('Login/login')."'</script>";
			}	 
			$t_agent=M('t_agent');	//代理表			
			$t_presented=M('t_presented');	//赠送数据表
			$xinxi=M('t_msg');	//信息表	
			$message=$xinxi->find();	//信息数据表			
			$sum_id=$t_presented->select(); //总的赠送数据信息
			$User_Profile=$t_agent->where('userid='.$_SESSION['userid'])->find();	//登录用户的信息
			$underling=$t_agent->where('referrer='.$User_Profile['accounts'])->Count();	//统计 推荐ID == 登录的ID的数量
			$cur_date= strtotime(date('Y-m-d'));
			$sum=$t_presented->where('benefactor='.$User_Profile['accounts'])->where('manner=1')->where("regtime >= '{$cur_date}'")->sum('gems');
			//得到  当前数据库时间戳大于当前时间戳的数据
			$this->assign('User_Profile',$User_Profile);	//传递数组
			$this->assign('message',$message);//传递数组
			$this->assign('sum',$sum);//传递数组
			$this->assign('underling',$underling);//传递数组
			$this->display();
		}
		/*---------充值页面---------*/
		public function searchplayer()
		{
			if(!isset($_SESSION['accounts']))
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('请登录');location.href='".U('Login/login')."'</script>";
				}
				$this->display();
		}
		/*---------信息页面---------*/
		public function chargegoldtoplayer()
		{
			if(!isset($_SESSION['accounts']))
			{
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('请登录');location.href='".U('Login/login')."'</script>";
			}    
				if(IS_POST)
				{
					if($_POST['userid']=="" || $_POST['userid']==0)
					{
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('您好,请正确输入您要赠送玩家的ID');location.href='".U('Index/searchplayer')."'</script>";
					}
					$userid=$_POST['userid'];
					$sql=M("","","DB_CONFIG1")->query('select * from t_users where userid='.$userid);//SQl 语句
				} 
				$this->assign('sql',$sql);
				$this->display();
		}
		/*---------充值处理---------*/
		public function chongzhi()
		{
			if(IS_POST){
				if($_POST['gems2']<=0)
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('请输入正确的充值数量');history.go(-1)</script>";
				}else
				{
				$userid=$_POST['userid'];
				$games_users=M("","","DB_CONFIG1")->query('select * from t_users where userid='.$userid); //游戏用户信息

				$agent=M('','','DB_CONFIG0')->query('select *from t_agent where userid='.$_SESSION['userid']);	//代理信息
				if($_POST['gems2']>$agent[0]['gems']){
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('您好，您的账号上钻石数不足，无法充值，请联系上级或 自行充值');history.go(-1)</script>";die;
				}				
				$sum=$games_users[0]['gems']+$_POST['gems2'];//充值后的玩家钻石数量
				$remain=$agent[0]['gems']-$_POST['gems2'];//充值后总代理的钻石数量
				$new_gems=M("","","DB_CONFIG1")->execute("update t_users set gems='$sum' where userid=".$userid);
				if($new_gems)
				{
					$new=M('','','DB_CONFIG0')->execute("update t_agent set gems='$remain' where userid=".$_SESSION['userid']);
					if($new)
					{
						$regtime=time();
						$benefactor=$agent[0]['accounts'];
						$manner=2;
						$recipient=$games_users[0]['userid'];
						$gems2=$_POST['gems2'];
						$game=$_POST['game'];
						$name=$_POST['name'];
						$gems_front=$agent[0]['gems'];//代理充值前的钻石
						$gems_queen=$agent[0]['gems']-$_POST['gems2'];//充值后总代理的钻石数量
						$accept_front=$games_users[0]['gems'];//游戏用户被充值前的钻石
						$accept_queen=$games_users[0]['gems']+$_POST['gems2'];//充值后的玩家钻石数量
						$sql=M('','','DB_CONFIG0')->execute("insert into t_donate(regtime,manner,benefactor,recipient,gems,game,name,gems_front,gems_queen,accept_front,accept_queen)values('$regtime','$manner','$benefactor','$recipient','$gems2','$game','$name','$gems_front','$gems_queen','$accept_front','$accept_queen')");
						if($sql)
						{
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$games_users[0]['name']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/searchplayer')."'</script>";die;
						}
					}
				}else
					{
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('系统繁忙,请重新充值');history.go(-1)</script>";
					}
				}
			}
			$this->display();
		}
		/*---------添加代理---------*/
		public function creatuser()
		{	   
		$t_agent=M('t_agent');
			$users=M('','','DB_CONFIG0')->query('select * from t_agent where accounts='.$_SESSION['accounts']);
			if(IS_POST){

				if($_POST['password'] != $_POST['passwordd'])
					{
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('两次密码不同，请重新输入');history.go(-1)</script>";die;
					}
				else
				{
				if($_POST['username']==""  || $_POST['password']=="" || $_POST['nickname']=="" || $_POST['referrer']=="" || $_POST['phone']=="" || $_POST['gametype']=="")
					{
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('请详细填写信息');history.go(-1)</script>";
					}
					else
					{
						$agencylv=$_POST['agencylv'];
						$gametype=$_POST['game'];
						do
						{
							$accounts = 10000+rand(1,9000)*rand(1,100);
							$accounts_1 = M('','','DB_CONFIG0')->query('select count(*) from t_agent where accounts = '.$accounts);
						}while ($accounts_1[0]['accounts'] > 0);	
						$username=$_POST['username'];
						$password=MD5($_POST['password']);
						$nickname=$_POST['nickname'];
						$region=$_POST['region'];		
						$referrer=$_POST['referrer'];
						$regtime=time();
						$phone=$_POST['phone']; //以上为数据接受
						$phone_1=$t_agent->where('phone='.$phone)->find();
						if($phone_1!="")
						{
							echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
							echo "<script type='text/javascript'>alert('此号码已存在，请重新输入');history.go(-1)</script>";die;
						}
						if($_SESSION['agencylv']>$agencylv)
						{
							echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
							echo "<script type='text/javascript'>alert('抱歉，您只能添加您的下级代理');history.go(-1)</script>";die;
						}
							$sql=M('','','DB_CONFIG0')->execute("insert into t_agent(agencylv,game,accounts,username,password,nickname,region,referrer,regtime,phone)values('$agencylv','$gametype','$accounts','$username','$password','$nickname','$region','$referrer','$regtime','$phone')");
						if($sql)
						{
							echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
							echo "<script type='text/javascript'>alert('代理添加成功');location.href='".U('Index/myadmin')."'</script>";die;
						}
						else
						{
							echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
							echo "<script type='text/javascript'>alert('填写的信息错误，请重新输入');history.go(-1)</script>";die;
						}
					}
				}
			}
			
		$this->assign('users',$users);
		$this->display();
		
		
		}
		/*---------分享链接页面---------*/
		public function addition()
		{
			$users=M('','','DB_CONFIG0')->query('select * from t_agent where accounts='.$_SESSION['accounts']);
			if(IS_POST){

				if($_POST['password'] != $_POST['passwordd'])
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('两次密码不同，请重新输入');history.go(-1)</script>";die;
				}else
				{
				if($_POST['username']==""  || $_POST['password']=="" || $_POST['nickname']=="" || $_POST['referrer']=="" || $_POST['phone']=="" || $_POST['gametype']=="")
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('请详细填写信息');history.go(-1)</script>";
				}
				else
					{
					$agencylv=$_POST['agencylv'];
					$gametype=$_POST['game'];
					do
					{
						$accounts = 1110000+rand(1,99999)*rand(2,99);
						$accounts_1=M('t_agent')->where('accounts='.$accounts)->select();
					}while ($accounts_1 != "");	
						$username=$_POST['username'];
						$password=MD5($_POST['password']);
						$nickname=$_POST['nickname'];
						$region=$_POST['region'];		
						$referrer=$_POST['referrer'];
						$regtime=time();
					//	$ip=get_client_ip();
						$phone=$_POST['phone']; //以上为数据接受
						if($_SESSION['agencylv']>$agencylv)
						{
							echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
							echo "<script type='text/javascript'>alert('抱歉，您只能添加您的下级代理');history.go(-1)</script>";die;
						}
						$sql=M('','','DB_CONFIG0')->execute("insert into t_agent(agencylv,game,accounts,username,password,nickname,region,referrer,regtime,phone)values('$agencylv','$gametype','$accounts','$username','$password','$nickname','$region','$referrer','$regtime','$phone')");
						if($sql)
						{
							echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
							echo "<script type='text/javascript'>alert('代理添加成功');location.href='".U('Index/myadmin')."'</script>";die;
						}
						else
						{
							echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
							echo "<script type='text/javascript'>alert('填写的信息错误，请重新输入');history.go(-1)</script>";die;
						}
					}
				}
			}
			
		$this->assign('users',$users);
		$this->display();
		
		}
		/*---------我的代理---------*/
		public function myadmin()
		{
			 if(!isset($_SESSION['accounts']))
			 {
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('请登录');location.href='".U('Login/login')."'</script>";
			 }
			$daili=M('t_agent');
			$one=$daili->where('accounts='.$_SESSION['accounts'])->find(); 
			$t_agent=M("","t_agent","DB_CONFIG0");
			if($one['agencylv']==0){
				$sql=M('','','DB_CONFIG0')->query("select * from t_agent where referrer!=0");
				if(IS_POST)
				{
					if($_POST['accounts']=="" && $username=="" AND $_POST['type']!="")
						{
						
					if($_POST['type']==1)
						{
							$sql=M('','','DB_CONFIG0')->query('select * from t_agent where referrer!=0  order by userid asc');
						}
					if($_POST['type']==2)
						{
							$sql=M('','','DB_CONFIG0')->query('select * from t_agent where referrer!=0   order by gems desc');
						}
					if($_POST['type']=="")
						{
							$sql=M('','','DB_CONFIG0')->query('select * from t_agent where referrer!=0');
						}
						}
				}
				if($_POST['accounts']!="" || $_POST['username']!="" && $_POST['type']=="")
				{
					$w['accounts']=array('like','%'.$_POST['accounts'].'%');//把数据写到空数组去
					$w['username']=array('like','%'.$_POST['username'].'%');//把数据写到空数组
					//$sql=M('','','DB_CONFIG0')->query("SELECT * FROM t_agent where referrer!=0 accounts LIKE '%".$userid_id."%' and username like '%".$userid_name."%'");
					$sql=$t_agent->where($w,'referrer!=0')->select();
				}
				if($_POST['accounts']=="" && $_POST['username']=="" && $_POST['type']==""  && $_POST['sdate']!="" || $_POST['edate']!="")
				{
					$sdate=strtotime($_POST['sdate']);
					$edate=strtotime($_POST['edate']);
					$arr['regtime']=array('egt',$sdate);
					$arr['regtime']=array('elt',$edate);
					$sql=$daili->where($arr,'referrer!=0')->select();//echo $daili->_sql();
				}
			}
			
			/*               当用户的等级不是总权限的时候               */
			if($one['agencylv']>0)
			{
				$sql=M('','','DB_CONFIG0')->query("select * from t_agent where referrer=".$one['accounts']);//查出 userid 不等于登录庄户的ID 的数据
			if(IS_POST)
			{
				if($_POST['accounts']=="" && $username=="" AND $_POST['type']!="")
					{
						$sql=M('','','DB_CONFIG0')->query('select * from t_agent where  referrer='.$one['accounts']);
					}
				if($_POST['type']==1)
					{
						$sql=M('','','DB_CONFIG0')->query('select * from t_agent where  referrer='.$one['accounts'].' order by userid asc');
					}
				if($_POST['type']==2)
					{
						$sql=M('','','DB_CONFIG0')->query('select * from t_agent where  referrer='.$one['accounts'].' order by gems desc');
					}
				if($_POST['type']=="")
					{
						$sql=M('','','DB_CONFIG0')->query('select * from t_agent where  referrer='.$one['accounts']);
					}
			}
				if($_POST['accounts']!="" || $_POST['username']!="" && $_POST['type']=="")
				{
					$userid_id=$_POST['accounts'];
					$userid_name=$_POST['username'];
					$sql=M('','','DB_CONFIG0')->query("SELECT * FROM t_agent WHERE accounts LIKE '%".$userid_id."%' and username like '%".$userid_name."%' AND referrer=".$one['accounts']);
				}
				if($_POST['accounts']=="" && $_POST['username']=="" && $_POST['type']==""  && $_POST['sdate']!="" || $_POST['edate']!="")
				{
					$sdate=strtotime($_POST['sdate']);
					$edate=strtotime($_POST['edate']);
					$arr['regtime']=array('egt',$sdate);
					$arr['regtime']=array('elt',$edate);
					$sql=$daili->where($arr,'referrer!='.$one['accounts'])->select();//echo $daili->_sql();
				}
			}
				$this->assign('one',$one);
				$this->assign('sql',$sql);
				$this->display();
		}
		
		/*---------我的返利---------*/
		public function reward()
		{
			if(!isset($_SESSION['accounts']))
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('请登录');location.href='".U('Login/login')."'</script>";
				}
			$t_rebate=M('t_rebate');
			$daili=M('t_agent');
			$t_recharge=M('t_recharge');
			$rebate=$t_rebate->where('recipient='.$_SESSION['accounts'])->order('state asc')->select();
			$one=$daili->where('accounts='.$_SESSION['accounts'])->find();
			if($one['agencylv']==0)
			{
				$rebate=$t_rebate->select();
			}
			if(IS_POST)
			{
				if($_POST['accounts']=="" && $_POST['type']!="" &&$_POST['edate']=="" && $_POST['sdate']=="")
				{				
					if($_POST['type']==1)
					{
						$rebate=M("","","DB_CONFIG0")->query('select * from t_rebate where recipient='.$_SESSION['accounts'].' AND superior_state=1');
						if($one['agencylv']==0)
						{
							$rebate=$t_rebate->where('state=1')->select();
						}
					}
					if($_POST['type']==0)
					{
						$rebate=M("","","DB_CONFIG0")->query('select * from t_rebate where recipient='.$_SESSION['accounts'].' AND superior_state=0');
						if($one['agencylv']==0)
						{
							$rebate=$t_rebate->where('state=0')->select();
						}
					}
					if($_POST['type']==2)
					{
						$rebate=$t_rebate->where('recipient='.$one['accounts'])->order('state asc')->select();
						if($one['agencylv']==0)
						{
							$rebate=$t_rebate->select();
						}
					}
				}
				if($_POST['type']=="" && $_POST['accounts']=="" && $_POST['sdate']!="" && $_POST['edate']!="")
				{
					$arr['times']=array('egt',$_POST['sdate']);
					$arr['times']=array('elt',$_POST['edate']);
					$rebate=$t_rebate->where($arr,'recipient='.$one['accounts'])->select();  //存到数组进行查询
					if($one['agencylv']==0)
					{
						$rebate=$t_rebate->where($arr)->select();  //存到数组进行查询
					}
				}
			}
				$this->assign('rebate',$rebate);
				$this->assign('one',$one);
				$this->display();
		}
		/*         我下级给我的返利         */
		public function subordinate()
		{
			if(!isset($_SESSION['accounts']))
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('请登录');location.href='".U('Login/login')."'</script>";
				}
				$t_rebate=M('t_rebate');
				$daili=M('t_agent');
				$t_recharge=M('t_recharge');
				$rebate=$t_rebate->where('superior_id='.$_SESSION['accounts'])->order('state asc')->select();
				$one=$daili->where('accounts='.$_SESSION['accounts'])->find();
			if($one['agencylv']==0)
			{
				$rebate=$t_recharge->select();
			}
			if(IS_POST)
			{
				if($_POST['accounts']=="" && $_POST['type']!="" &&$_POST['edate']=="" && $_POST['sdate']=="")
				{				
				if($_POST['type']==1)
					$rebate=M("","","DB_CONFIG0")->query('select * from t_rebate where superior_id='.$_SESSION['accounts'].' AND superior_state=1');
				}
				if($_POST['type']==0)
				{
					$rebate=M("","","DB_CONFIG0")->query('select * from t_rebate where superior_id='.$_SESSION['accounts'].' AND superior_state=0');

				}
				if($_POST['type']==2)
				{
					$rebate=M("","","DB_CONFIG0")->query('select * from t_rebate where superior_id='.$_SESSION['accounts']);
				}
			}
				if($_POST['type']=="" && $_POST['accounts']=="" && $_POST['sdate']!="" && $_POST['edate']!="")
				{
					$arr['times']=array('egt',$_POST['sdate']);
					$arr['times']=array('elt',$_POST['edate']);
					$rebate=$daili->where($arr,'superior_id='.$_SESSION['accounts'])->select();  //存到数组进行查询
				}
				$this->assign('rebate',$rebate);
				$this->assign('one',$one);
				$this->display();
		}
		/*-------------返利结算------------------*/
		public function settle(){
			$daili=M('t_rebate');
			$t_agent=M('t_agent');
			$id=$_GET['id'];
			$one=1;
			$rebate=$daili->where('id='.$id)->find();//登录用户可以返利的钻石die;
			$message=$t_agent->where('accounts='.$_SESSION['accounts'])->find();//登录用户的基本信息]
			$sum=$rebate['accumulative']+$message['gems'];
			if(M("","","DB_CONFIG0")->execute("update t_agent set gems='$sum' where accounts=".$message['accounts']))
			{
				M("","","DB_CONFIG0")->execute("update t_rebate set state='$one' where id=".$id);
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('领取成功');location.href='".U('Index/reward')."'</script>";
			}else
			{
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('系统繁忙，请您重再次领取');location.href='".U('Index/reward')."'</script>";
			}
		}
			/*-------------全部领取结算------------------*/
		public function settle_2()
		{
			$daili=M('t_rebate');
			$t_agent=M('t_agent');
			$id=$_GET['id'];
			$one=1;
			//$rebate=M("","","DB_CONFIG0")->query('select * from t_rebate where benefactor='.$_SESSION['accounts'].' AND state=0');
			$rebate=$daili->where('state=0 AND benefactor='.$_SESSION['accounts'])->sum('accumulative');
			$message=$t_agent->where('accounts='.$_SESSION['accounts'])->find();//登录用户的基本信息]
			
			$sum=$rebate+$message['gems'];
			if(M("","","DB_CONFIG0")->execute("update t_agent set gems='$sum' where accounts=".$message['accounts']))
			{
				M("","","DB_CONFIG0")->execute("update t_rebate set state='$one' where benefactor=".$_SESSION['accounts']);
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('领取成功');location.href='".U('Index/reward')."'</script>";
			}else
			{
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('没有可以领取的对象');location.href='".U('Index/reward')."'</script>";
			}
		}
		/*-------------下级返利结算------------------*/
		public function superior_settle()
		{
			$daili=M('t_rebate');
			$t_agent=M('t_agent');
			$id=$_GET['id'];
			$one=1;
			$rebate=$daili->where('id='.$id)->find();//登录用户可以返利的钻石die;
			//dump($rebate);die;
			$message=$t_agent->where('accounts='.$_SESSION['accounts'])->find();//登录用户的基本信息]
			$sum=$rebate['superior']+$message['gems'];
			if(M("","","DB_CONFIG0")->execute("update t_agent set gems='$sum' where accounts=".$message['accounts']))
			{
				M("","","DB_CONFIG0")->execute("update t_rebate set superior_state='$one' where id=".$id);
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('领取成功');location.href='".U('Index/subordinate')."'</script>";
			}else
			{
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('系统繁忙，请您重再次领取');location.href='".U('Index/subordinate')."'</script>";
			}
		}
		
	/*-------------全部领取结算------------------*/
		public function superior_settle2(){
			$daili=M('t_rebate');
			$t_agent=M('t_agent');
			$id=$_GET['id'];
			$one=1;
			$rebate=$daili->where('superior_state=0 AND superior_id='.$_SESSION['accounts'])->sum('accumulative');
			$message=$t_agent->where('accounts='.$_SESSION['accounts'])->find();//登录用户的基本信息]
			$sum=$rebate+$message['gems'];
			if(M("","","DB_CONFIG0")->execute("update t_agent set gems='$sum' where accounts=".$message['accounts']))
			{
				M("","","DB_CONFIG0")->execute("update t_rebate set state='$one' where benefactor=".$_SESSION['accounts']);
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('领取成功');location.href='".U('Index/reward')."'</script>";
			}else
			{
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('没有可以领取的对象');location.href='".U('Index/subordinate')."'</script>";
			}
		}
		/*---------下级代理的充钻记录---------*/
		public function agentcharge()
		{
				if(!isset($_SESSION['accounts']))
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('请登录');location.href='".U('Login/login')."'</script>";
				}
					$daili=M('t_presented');
					if($_GET['accounts']=="")
					{
						$sql=$daili->where('up_id='.$_SESSION['accounts'])->order('regtime desc')->select();//SQL 查询语句
						if($_SESSION['agencylv']==0)
						{
							$sql=$daili->order('regtime asc')->select();
						}
						if(IS_POST)
						{		
							$sql=$daili->where('up_id='.$_SESSION['accounts'])->select();
							if(IS_POST)
							{
								if($_POST['accounts']=="" && $username=="" AND $_POST['type']!="")
								{
									$sql=$daili->where('up_id='.$_SESSION['accounts'])->select();
									if($_SESSION['agencylv']==0)
									{
										$sql=$daili->order('up_id asc')->select();
									}
								}
								if($_POST['type']==1)
								{
									$sql=$daili->where('up_id='.$_SESSION['accounts'])->order('regtime desc')->select();
									if($_SESSION['agencylv']==0){
										$sql=$daili->order('regtime asc')->select();
									}
								}
								if($_POST['type']==2)
								{
									$sql=$daili->where('up_id='.$_SESSION['accounts'])->order('gems desc')->select();
									if($_SESSION['agencylv']==0){
										$sql=$daili->order('gems asc')->select();
									}
								}
								if($_POST['type']==0)
								{
									$sql=$daili->where('up_id='.$_SESSION['accounts'])->order('benefactor asc')->select();//echo 
									if($_SESSION['agencylv']==0){
										$sql=$daili->order('up_id asc')->select();
									}
								}
							}
							
							if($_POST['accounts']!="" || $_POST['username']!="" && $_POST['type']=="")
							{
								$w['recipient']=array('like','%'.$_POST['accounts'].'%');//把数据写到空数组去
								$w['up_id']=array('like','%'.$_POST['username'].'%');//把数据写到空数组
								$sql=$daili->where($w)->where('up_id='.$_SESSION['accounts'])->select();
								if($_SESSION['agencylv']==0)
								{
									$sql=$daili->where($w)->select();
									//dump($sql);die;
								}
							}
							if($_POST['accounts']=="" && $_POST['username']=="" && $_POST['type']==""  && $_POST['sdate']!="" || $_POST['edate']!="")
							{
								$arr['regtime']=array('egt',$_POST['sdate']);
								$arr['regtime']=array('elt',$_POST['edate']);
								$sql=$daili->where($arr,'recipient='.$one['accounts'])->select();
								if($_SESSION['agencylv']==0){
									$sql=$daili->where($arr)->select();
								}
							}
						}
					}
					if($_GET['accounts']!="")
					{
						$sql=$daili->where('recipient='.$_GET['accounts'])->order('regtime desc')->select();
						if(IS_POST)
						{
							if($_POST['accounts']=="" && $username=="" AND $_POST['type']!="")
							{
							if($_POST['type']==1)
							{
								$sql=$daili->where('recipient='.$_GET['accounts'])->order('gems asc')->select();
							}
							if($_POST['type']==2)
							{
								$sql=$daili->where('recipient='.$_GET['accounts'])->order('regtime asc')->select();
							}
							if($_POST['type']=="")
							{
								$sql=$daili->where('recipient='.$_GET['accounts'])->order('recipient asc')->select();//echo 
							}
							}
							if($_POST['accounts']!="" || $_POST['username']!="" && $_POST['type']=="")
							{
								$w['recipient']=array('like','%'.$_POST['accounts'].'%');//把数据写到空数组去
								$w['up_id']=array('like','%'.$_POST['username'].'%');//把数据写到空数组
								$sql=$daili->where($w)->where('recipient='.$_GET['accounts'])->select();
							}
							if($_POST['accounts']=="" && $_POST['username']=="" && $_POST['type']==""  && $_POST['sdate']!="" || $_POST['edate']!="")
							{
								$arr['regtime']=array('egt',$_POST['sdate']);
								$arr['regtime']=array('elt',$_POST['edate']);
								$sql=$daili->where($arr,'recipient='.$_GET['accounts'])->select();
							}
						}
					}
				$this->assign('sql',$sql);
				$this->display();
		}
		/*---------更改密码---------*/
		public function modipass()
		{
			$daili=M('t_agent');
			$userid=$_GET['userid'];
			if(IS_POST)
			{
				if($_POST['ppass']=="" || $_POST['cword']=="")
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('
					密码不能为空');history.go(-1)</script>";
				}
				if($ppass!=$cword)
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('您输入的新密码和确认密码不一致，请重新输入');history.go(-1)</script>";
				}
				if(MD5($_POST['ppass'])==MD5($_POST['cword']))
				{
					$ppass=MD5($_POST['ppass']);
					if(M("","","DB_CONFIG0")->execute("update t_agent set password='$ppass' where userid=".$userid))
					{
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('密码重置成功，请保管好您的密码。');location.href='".U('Index/myadmin')."'</script>";
					}else
					{
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('系统繁忙，请您重再次输入');history.go(-1)</script>";
					}
				}
			}
			
			$this->display();
		}
		/*---------代理充值钻石页面---------*/
		public function ChargeGoldToAgent()
		{
			$daili=M('t_agent');
			$pre=M('t_presented');
			$userid=I('get.userid');  													//接受方		
			$singleAgentInfo=$daili->where('userid='.$userid)->find(); 					//接收方的数据
			$low=$daili->where('accounts='.$singleAgentInfo['referrer'])->find();		//接收方的上级数据
			$message=$daili->where('userid='.$_SESSION['userid'])->find(); 				//登录的用户数据
			$limits=$message['limits'];													//判定达到这个数的上限返利	
			$rebate=$message['rebate'];													//上级返利数量
			$up_rebate=$message['up_rebate'];											//上上级返利数量
			$superior=$daili->where('accounts='.$message['referrer'])->find();			//dump($superior	//登录用户的上级代理数据
			$gems2=$_POST['gems2'];														//要冲的钻石
			$new=$message['gems']-$gems2;												// 登录用户的钻石量减去要充值的钻石量加上返利的数量
			$up_id2=$daili->where('accounts='.$low['referrer'])->find();				//接收方的上上级数据
			$add=$singleAgentInfo['gems']+$gems2;										//用户增加后的钻石数量
			//dump($singleAgentInfo);die;
			//写入充值记录
										
			$date['manner']=$_POST['fangshi'];															
			$date['benefactor']=$message['accounts'];															
			$date['recipient']=$singleAgentInfo['accounts'];							
			$date['up_id']=$up_id2['accounts'];			
			$date['gems']=$_POST['gems2'];														
			$date['properties']=0;			
			$date['gems_front']=$message['gems'];			
			$date['gems_queen']=$new;			
			$date['accept_frone']=$singleAgentInfo['gems'];			
			$date['accept_queen']=$add;			
			$date['superior']=$singleAgentInfo['referrer'];		 	
			$date['sort']=0;	
			//被充值上级代理等于2级写入冲值表----------------*/
			
			$t_rebate = M('','t_rebate','DB_CONFIG0'); 
			$date2['benefactor']=$message['accounts'];									//充钻方式						
			$date2['recipient']=$low['accounts'];										//被充钻账号						
			$date2['gems']=$_POST['gems2'];												//充钻数量	
			$date2['up_id2']=$up_id2['accounts'];		
			$date2['up_id']=$singleAgentInfo['accounts'];		
			$date2['accumulative']=floor(($_POST['gems2']+$singleAgentInfo['surplus'])/$limits)*$rebate;	//返利		
			//被充值代理上级代理等于3写入返利
			
			$date3['benefactor']=$message['accounts'];	//充钻方式						
			$date3['recipient']=$low['accounts'];	//被充钻账号						
			$date3['gems']=$_POST['gems2'];	//充钻数量	
			$date3['accumulative']=floor(($_POST['gems2']+$singleAgentInfo['surplus'])/$limits)*$rebate;	//返利						
			$date3['superior']=floor(($_POST['gems2']+$singleAgentInfo['surplus'])/$limits)*$up_rebate;	//上级返利			
			$date3['superior_id']=$up_id2['accounts'];
			$date3['up_id']=$singleAgentInfo['accounts'];
			if(IS_POST)	
			{				
				if($_POST['gems2']<=0)
					{
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('您好，请输入正确的充值数量');history.go(-1)</script>";die;
					}
				if($gems2 > $message['gems'])	
					{		
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('您好，您的账号上钻石数不足，无法充值，请联系上级或自行充值');history.go(-1)</script>";die;
					}
				/*----------赠送----------*/
				if($_POST['fangshi']==2)
				{
					if($message['agencylv']!=0)
					{
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('抱歉，您的权限不足');history.go(-1)</script>";die;
					}
					if(($_POST['gems2'])%$limits==0)		//如果充值的等于50个
					{
						//当用户的等级未总权限的话  之扣除钻石 写入充钻记录  不返利
						if($low['agencylv']==0)
						{
							$daili->where('accounts='.$message['accounts'])->setField('gems',$new); //更改登录用户的钻石，充值后
							$daili-> where('userid='.$userid)->setField('gems',$add);  //玩家被充值后

							if($pre->add($date))
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
								}
							else
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
								}								
						}
						else if($low['agencylv']==1)
						{
							$daili->where('accounts='.$message['accounts'])->setField('gems',$new); //更改登录用户的钻石，充值后
							$daili-> where('userid='.$userid)->setField('gems',$add);  //玩家被充值后	
							if((floor(($_POST['gems2']+$message['surplus'])/$limits)*$rebate)!=0) 
							{					
								$t_rebate->add($date2);	
								if($pre->add($date))
									{
										echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
										echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
									}
								else
									{
										echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
										echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
									}
							}	
							if((floor(($_POST['gems2']+$message['surplus'])/$limits)*$rebate)==0)
							{															
								if($pre->add($date))
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
								}else
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
								}	
							}							
						}
						else if($low['agencylv']>=2)
						{	
							$daili->where('accounts='.$message['accounts'])->setField('gems',$new); //登录用户的钻石量									
							$daili-> where('userid='.$userid)->setField('gems',$add);									
							if((floor(($_POST['gems2']+$message['surplus'])/$limits)*$rebate)==0)
							{												
								if($pre->add($date))
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
								}else
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
								}	
							}	
							else
							{					
								$t_rebate->add($date3);
								if($pre->add($date))
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
								}else
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
								}
							}
						}
					}
				}
				if($_POST['gems2']<$limits)
				{
					if($low['agencylv']==0)
					{
						$daili->where('accounts='.$message['accounts'])->setField('gems',$new); //更改登录用户的钻石，充值后
						$daili-> where('userid='.$userid)->setField('gems',$add);  //玩家被充值后
					if($pre->add($date))
						{
							echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
							echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
						}
					else
						{
							echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
							echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
						}								
					}
					if($low['agencylv']==1)
						{
							$daili->where('accounts='.$message['accounts'])->setField('gems',$new); //更改登录用户的钻石，充值后
							$daili-> where('userid='.$userid)->setField('gems',$add);  //玩家被充值后
							if((floor(($_POST['gems2']+$singleAgentInfo['surplus'])/$limits)*$rebate)==0)
							{
									$sum_gems_sum=$_POST['gems2']+$singleAgentInfo['surplus'];
									M("","","DB_CONFIG0")->execute("update t_agent set surplus='$sum_gems_sum' where accounts=".$singleAgentInfo['accounts']);		//需要乘以50的倍数，
									$daili->where('accounts='.$message['accounts'])->setField('gems',$new); //登录用户减去充值的钻石	
									$daili-> where('userid='.$userid)->setField('gems',$singleAgentInfo['gems']+$gems2);	//接收方的钻石		
								if($pre->add($date))
									{
										echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
										echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
									}
								else
									{
										echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
										echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
									}	
							}	
							//如果充值的钻石加上累计的钻石 不等于50
						if((floor(($_POST['gems2']+$singleAgentInfo['surplus'])/$limits)*$rebate)!=0) 
							{	
								$t_rebate->add($date2);
								$sum_gems_sum2=($_POST['gems2']+$singleAgentInfo['surplus'])%$limits;
								M("","","DB_CONFIG0")->execute("update t_agent set surplus='$sum_gems_sum2' where accounts=".$singleAgentInfo['accounts']);		//需要乘以50的倍数	
							
							if($pre->add($date))
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
								}
								else
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
								}
							}
						}
					if($low['agencylv']>=2)
					{  
						if(($_POST['gems2']+$singleAgentInfo['surplus'])%$limits == 0)  //如果累积的钻石和要充值的钻石加起来  % 50  等于0
						{ 
							$sum_gems_sum=($_POST['gems2']+$singleAgentInfo['surplus'])%$limits;
							M("","","DB_CONFIG0")->execute("update t_agent set surplus='$sum_gems_sum' where accounts=".$singleAgentInfo['accounts']);
							$daili->where('accounts='.$message['accounts'])->setField('gems',$new); //登录用户减去充值的钻石加上返利的钻石		+2	
							$daili-> where('userid='.$userid)->setField('gems',$singleAgentInfo['gems']+$gems2);	//接收方的钻石
								
							if((floor(($_POST['gems2']+$singleAgentInfo['surplus'])/$limits)*$rebate)==0)
							{								
								if($pre->add($date))
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
								}else
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
								}	
							}
							if((floor(($_POST['gems2']+$singleAgentInfo['surplus'])/$limits)*$rebate)!=0) 
							{ 
								$t_rebate->add($date3);
								if($pre->add($date))
								{	
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
								}
								else
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
								}
							}
						}
						if($_POST['gems2']+$singleAgentInfo['surplus']<$limits)
						{	
							$sum_gems_sum=$_POST['gems2']+$singleAgentInfo['surplus'];
							$daili->where('accounts='.$message['accounts'])->setField('gems',$new); //更改登录用户的钻石，充值后
							$daili-> where('userid='.$userid)->setField('gems',$singleAgentInfo['gems']+$gems2);	//接收方的钻石
							M("","","DB_CONFIG0")->execute("update t_agent set surplus='$sum_gems_sum' where accounts=".$singleAgentInfo['accounts']);
							if($pre->add($date))
							{
								echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
								echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
							}
							else
							{
								echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
								echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
							}
						}
					}
				}
			if($_POST['gems2']+$singleAgentInfo['surplus'] > $limits)  //如果累积的钻石和要充值的钻石加起来  % 50  等于0
				{		
					if($low['agencylv']==0)
					{
						$daili-> where('userid='.$userid)->setField('gems',$add);  //玩家被充值后
						if($pre->add($date))
						{
							echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
							echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
						}
						else
						{
							echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
							echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
						}								
					}
					if($low['agencylv']==1)
					{					
							$daili-> where('userid='.$userid)->setField('gems',$singleAgentInfo['gems']+$gems2);										//接收方的钻石
							$sum_gems_sum=($_POST['gems2']+$singleAgentInfo['surplus'])%$limits;																//要冲的钻石+存的钻石  %50
							M("","","DB_CONFIG0")->execute("update t_agent set surplus='$sum_gems_sum' where accounts=".$singleAgentInfo['accounts']);	//被充值用户存起来的钻石
							M("","","DB_CONFIG0")->execute("update t_agent set gems='$new' where accounts=".$message['accounts']); 						//充值方充值后的钻石
						if(floor(($_POST['gems2']+$singleAgentInfo['surplus'])%$limits)!=0)
						{	
							$t_rebate->add($date2);									
							if($pre->add($date))
							{
								echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
								echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
							}else
							{
								echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
								echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
							}	
						}
						if(floor(($_POST['gems2']+$singleAgentInfo['surplus'])%$limits)==0)
						{	
								//写入返利记录
							$sum_gems_sum=($_POST['gems2']+$singleAgentInfo['surplus'])%$limits;
							M("","","DB_CONFIG0")->execute("update t_agent set surplus='$sum_gems_sum' where accounts=".$singleAgentInfo['accounts']);	//被充值用户存起来的钻石
							$t_rebate->add($date2);									
								//写入充值记录
							if($pre->add($date))
							{
								echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
								echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
							}else
							{
								echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
								echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
							}	
						}
					}
					if($low['agencylv']>=2)
					{	
						$daili-> where('userid='.$userid)->setField('gems',$singleAgentInfo['gems']+$gems2);									//接收方的钻石
						$sum_gems_sum=($gems2+$singleAgentInfo['surplus'])%$limits;															//要冲的钻石+存的钻石  %50
						M("","","DB_CONFIG0")->execute("update t_agent set surplus='$sum_gems_sum' where accounts=".$singleAgentInfo['accounts']);		//存起来的钻石
						M("","","DB_CONFIG0")->execute("update t_agent set gems='$new' where accounts=".$message['accounts']);					//登录用户减去充值的钻石加上返利的钻石			
								/*-----------写入返利表----------------*/
						if((floor(($gems2+$singleAgentInfo['surplus'])/$limits)*$rebate)!=0) 
						{
							$t_rebate->add($date3);
							if($pre->add($date))
							{
								echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
								echo "<script type='text/javascript'>alert('您已赠送给玩家 ".$singleAgentInfo['username']." 钻石 ".$gems2." 个,已成功');location.href='".U('Index/myadmin')."'</script>";die;
							}else
							{
								echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
								echo "<script type='text/javascript'>alert('系统繁忙，请您重再次充值');history.go(-1)</script>";die;
							}
						}										
					}										
				}
			}			
								$this->assign('singleAgentInfo',$singleAgentInfo);
								$this->display();

		}
			
			

		
		/*---------我的购钻记录---------*/
		public function buygold()
		{   
			if(!isset($_SESSION['accounts']))
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('请登录');location.href='".U('Login/login')."'</script>";
				}				
				$daili=M('t_presented');
				$t_agent=M('t_agent');
				$sql=$daili->where('recipient='.$_SESSION['accounts'])->select();	//查询出所有的数据
				$one=$t_agent->where('accounts='.$_SESSION['accounts'])->find();
				
				if($one['agencylv']==0)
				{
					$sql=$daili->select();
				}
				if($_GET['accounts']!="")	
				{
					if(IS_POST)
					{

						if($_POST['type']!="" AND $_POST['sdate']!="" AND $_POST['edate']!="")
						{
							if($_POST['type']==1)//传过来的查询方式为1 要执行的
								{
									$sdate=strtotime($_POST['sdate']); //POST来的时间转为时间戳
									$edate=strtotime($_POST['edate']);	//POST来的时间转为时间戳
									$arr['regtime']=array('egt',$sdate);	//判断小于等于起始时间的条件
									$arr['regtime']=array('elt',$edate);	//判断大于等于起始时间的条件
									$sql=$daili->where($arr,'manner==1')->where('recipient='.$sql['accounts'])->select();
								if($one['agencylv']==0)
									{
										$sql=$daili->where($arr,'manner==1')->select();
									}
								}
							if($_POST['type']==2)  //传过来的查询方式为2 要执行的
							{
								$sdate=strtotime($_POST['sdate']); //POST来的时间转为时间戳
								$edate=strtotime($_POST['edate']);	//POST来的时间转为时间戳
							if($sdate<$edate || $sdate=="" AND $edate!=="" || $sdate!=="" AND $sdate=="")
								{
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo "<script type='text/javascript'>alert('查询时间错误');location.go(-1)</script>";die;
								}
								$arr['regtime']=array('egt',$sdate);	//判断小于等于起始时间的条件
								$arr['regtime']=array('elt',$edate);	//判断大于等于起始时间的条件
							if($one['agencylv']==0)
								{
									$sql=$daili->where($arr,'manner==2')->select();
								}
								$sql=$daili->where($arr,'manner='.$_POST['type'])->select();
							}
						}
						if($_POST['type']!=""  AND $_POST['sdate']==""  AND $_POST['edate']=="")
						{
							if($_POST['type']==0)
							{
								$sql=$daili->where('recipient='.$_SESSION['accounts'])->select();
								if($one['agencylv']==0)
								{
									$sql=$daili->where($arr,'manner==1')->select();
								}
							}
							if($_POST['type']==1)//传过来的查询方式为1 要执行的
							{
								$sql=$daili->where('manner='.$_POST['type'])->where('recipient='.$_SESSION['accounts'])->select();
								if($one['agencylv']==0)
								{
									$sql=$daili->where($arr,'manner==0')->select();
								}
							}
							if($_POST['type']==2)  //传过来的查询方式为2 要执行的
							{
								$sql=$daili->where('manner='.$_POST['type'])->where('recipient='.$_SESSION['accounts'])->select();
								if($one['agencylv']==0){
									$sql=$daili->where($arr,'manner==2')->select();
								}
							}	
						}
						if($_POST['type']==0 AND $_POST['sdate']!="" AND $_POST['edate']!="")
						{
							$sdate=$_POST['sdate']; //POST来的时间转为时间戳
							$edate=$_POST['edate'];	//POST来的时间转为时间戳
							$arr['regtime']=array('EGT',$sdate);	//判断小于等于起始时间的条件
							$arr['regtime']=array('ELT',$edate);	//判断大于等于起始时间的条件
							$sql=$daili->where($arr)->where($arr,'manner='.$_POST['type'])->where('recipient='.$_SESSION['accounts'])->select();
							if($one['agencylv']==0)
							{
									$sql=$daili->where($arr)->select();
							}
						}
					}
				}
				if($_GET['accounts']=="")	
				{
				if(IS_POST)
				{

						if($_POST['type']!="" AND $_POST['sdate']!="" AND $_POST['edate']!="")
						{
							if($_POST['type']==1)//传过来的查询方式为1 要执行的
								{
									$sdate=$_POST['sdate']; //POST来的时间转为时间戳
									$edate=$_POST['edate'];	//POST来的时间转为时间戳
									$arr['regtime']=array('egt',$sdate);	//判断小于等于起始时间的条件
									$arr['regtime']=array('elt',$edate);	//判断大于等于起始时间的条件
									$sql=$daili->where($arr,'manner==1')->select();
									if($one['agencylv']==0){
										$sql=$daili->where($arr,'manner==1')->select();
									}
								}
							if($_POST['type']==2)  //传过来的查询方式为2 要执行的
								{
									$sdate=$_POST['sdate']; //POST来的时间转为时间戳
									$edate=$_POST['edate'];	//POST来的时间转为时间戳
								if($sdate<$edate || $sdate=="" AND $edate!=="" || $sdate!=="" AND $sdate==""){
										echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
										echo "<script type='text/javascript'>alert('查询时间错误');location.go(-1)</script>";die;
									}
									$arr['regtime']=array('egt',$sdate);	//判断小于等于起始时间的条件
									$arr['regtime']=array('elt',$edate);	//判断大于等于起始时间的条件
									$sql=$daili->where($arr,'manner='.$_POST['type'])->select();
									if($one['agencylv']==0){
										$sql=$daili->where($arr,'manner==2')->select();
									}
								}
						}
						if($_POST['type']!=""  AND $_POST['sdate']==""  AND $_POST['edate']=="")
						{
							if($_POST['type']==0)
								{
									$sql=$daili->select();
									if($one['agencylv']==0){
										$sql=$daili->where($arr,'manner==0')->select();
									}
								}
							if($_POST['type']==1)//传过来的查询方式为1 要执行的
								{
									$sql=$daili->where('manner='.$_POST['type'])->select();
									if($one['agencylv']==1){
										$sql=$daili->where($arr,'manner==1')->select();
									}
								}
							if($_POST['type']==2)  //传过来的查询方式为2 要执行的
								{
									$sql=$daili->where('manner='.$_POST['type'])->select();
									if($one['agencylv']==2){
										$sql=$daili->where($arr,'manner==1')->select();
									}
								}	
						}
						if($_POST['type']==0 AND $_POST['sdate']!="" AND $_POST['edate']!="")
							{
								$sdate=$_POST['sdate']; //POST来的时间转为时间戳
								$edate=$_POST['edate'];	//POST来的时间转为时间戳
								$arr['regtime']=array('EGT',$sdate);	//判断小于等于起始时间的条件
								$arr['regtime']=array('ELT',$edate);	//判断大于等于起始时间的条件
								$sql=$daili->where($arr)->where($arr,'manner='.$_POST['type'])->select();
								if($one['agencylv']==0){
										$sql=$daili->where($arr)->select();
									}
							}
				}
				}	
				
				
				$this->assign('sql',$sql);
				
				$this->display();
		}
		/*---------修改代理信息---------*/
		public function agentmodi()
		{
			$daili=M('t_agent');
			$userid=I('get.userid');
			$sql=$daili->where('userid='.$userid)->find();	//SQL查询语句
			
			if($_POST){
				if($_POST['username']=="" || $_POST['nickname']=="" || $_POST['region']=="" )
				{
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('请详细填写信息');history.go(-1)</script>";die;
				}
				$daili->username=$_POST['username'];
				$daili->nickname=$_POST['nickname'];
				$daili->region=$_POST['region'];
				if($_POST['username']!="" || $_POST['nickname']!="" || $_POST['region']!="" )
				{
				if($daili->where('userid='.$userid)->save())
					{
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('修改信息成功');location.href='".U('Index/myadmin')."'</script>";
					}
				else
					{
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('您并未修改信息，请返回');history.go(-1)</script>";
					}
				}
			}	
			$this->assign('sql',$sql);
			$this->display();
		}	
		/*---------赠送玩家记录---------*/
		public function playercharge(){
			if(!isset($_SESSION['accounts']))
				{
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('请登录');location.href='".U('Login/login')."'</script>";
				}
			$daili=M('t_donate');
			$sql=$daili->where('benefactor='.$_SESSION['accounts'])->order('regtime desc')->select();//要查询的数据
			if($_SESSION['agencylv']==0)
					{
						$sql=$daili->select();
					}
				
			if(IS_POST){
				if($_POST['accounts']=="" && $username=="" AND $_POST['type']!="")
				{
					$sql=$daili->where('benefactor='.$_SESSION['accounts'])->order('accounts desc')->select();//echo $daili->_sql();die;
				if($_SESSION['agencylv']==0)
					{
						$sql=$daili->order('accounts desc')->select();
					}
				}
				}
				if($_POST['type']==1)
				{
					$sql=$daili->where('benefactor='.$_SESSION['accounts'])->order('accounts asc')->select();//echo $daili->_sql();die;
				if($_SESSION['agencylv']==0)
					{
						$sql=$daili->order('accounts desc')->select();
					}
				}
				
				if($_POST['type']==2)
				{
					$sql=$daili->where('benefactor='.$_SESSION['accounts'])->order('gems asc')->select();//echo $daili->_sql();die;
				if($_SESSION['agencylv']==0)
					{
						$sql=$daili->order('gems desc')->select();
					}
				}
				if($_POST['type']=="")
				{
					$sql=$daili->where('benefactor='.$_SESSION['accounts'])->order('recipient asc')->select();//echo $daili->_sql();die;
				if($_SESSION['agencylv']==0)
					{
						$sql=$daili->order('recipient desc')->select();
					}
				}
				if($_POST['accounts']!="" || $_POST['username']!="" && $_POST['type']=="")
				{
					$w['accounts']=array('like','%'.$accounts.'%');//把数据写到空数组去
					$w['username']=array('like','%'.$username.'%');//把数据写到空数组
					$sql=$daili->where($w,'benefactor='.$_SESSION['accounts'])->order('regtime desc')->select();//echo $daili->_sql();die;
					if($_SESSION['agencylv']==0)
					{
						$sql=$daili->where($w)->order('regtime desc')->select();
					}
					if(!isset($sql)){
						echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('搜索内容不存在');history.go(-1)</script>";
					}
				}
				if($_POST['accounts']=="" && $_POST['username']=="" && $_POST['type']==""  && $_POST['sdate']!="" || $_POST['edate']!="")
				{
					$arr['regtime']=array('egt',strtotime($_POST['sdate']));
					$arr['regtime']=array('elt',strtotime($_POST['edate']));
					$sql=$daili->where($arr,'benefactor='.$_SESSION['accounts'])->select();
				if($_SESSION['agencylv']==0)
					{
						$sql=$daili->where($w)->order('regtime desc')->select();
					}
				}
				$this->assign('sql',$sql);
				$this->display();
			}
			/*---------代理的信息---------*/
		public function myinfo(){
			      if(!isset($_SESSION['accounts']))  //判断是句是否存在
				  {
			echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
			echo "<script type='text/javascript'>alert('请登录');location.href='".U('Login/login')."'</script>";
				  }
		
			$daili=M('t_agent');
			if($_GET['accounts']==""){
				$sql=$daili->where('accounts='.$_SESSION['accounts'])->find();//sql查询后的结果
			}else{
				$sql=$daili->where('accounts='.$_GET['accounts'])->find();
			}
			$this->assign('sql',$sql);
			$this->display();
			}
			/*---------消息通知---------*/
		public function setnotice()
			{
			       if(!isset($_SESSION['accounts'])){
			echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
			echo "<script type='text/javascript'>alert('请登录');location.href='".U('Login/login')."'</script>";
			}
			$daili=M('t_msg');
			$sql=$daili->find();//sql查询语句
			if(IS_POST){
				$daili->content=$_POST['content'];
				$bb=$daili->save();
				if($bb)
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('修改成功');location.href='".U('index/indexx')."'</script>";
				}else{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('修改失败，请重新修改');location.href='".U('index/indexx')."'</script>";
				}
			}
			$this->assign('sql',$sql);
			$this->display();
			}	
			/*---------代理政策---------*/
		public function policy(){
				$daili=M('t_msg');
				$sql=$daili->find();//sql查询语句

				$this->assign('sql',$sql);
				$this->display();
				
			}		
			/*---------政策管理---------*/
		public function policyedit()
		{
		    if(!isset($_SESSION['accounts']))
		    {
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('请登录');location.href='".U('Login/login')."'</script>";
			}
			if(IS_POST){
			
			//上传文件
			$imgsrc="";
			if($_FILES['file']['error']==0){
				$imgsrc=$this->uploadOne($_FILES['file']);				
			}
			$shu=M('t_msg');
			//dump($shu);die;
			$shu->tupian=$imgsrc;
			if($shu->add()){
				$this->redirect('Index/policy');die;
			}else{
				$this->error('添加失败');die;
			}
		}

		   
			$this->display();
		}	

	public function uploadOne($tupian){   
		 $upload = new \Think\Upload();// 实例化上传类    
		 $upload->maxSize   =     3145728 ;// 设置附件上传大小    
		 $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
		 $upload->savePath  =      ''; // 设置附件上传目录    // 上传单个文件     
	$info   =   $upload->uploadOne($tupian);
 
		 if(!$info) {// 上传错误提示错误信息            
		 $this->error(
			$upload->getError()
		 );       
		 }else{// 上传成功 获取上传文件信息    
			return $info['savepath'].$info['savename']; 
			}
	 }  
	
		//支付页面	
	public function payoff()
	{		
		$this->display(); 
	}	
	public function payoff_jump()
	{
		$state=trim($_GET["state"]);            	// 1:充值成功 2:充值失败
		$customerid=trim($_GET['customerid']);		//商户注册的时候，网关自动分配的商户ID
		$sd51no=trim($_GET["sd51no"]);          	//该订单在网关系统的订单号
		$sdcustomno=trim($_GET["sdcustomno"]);  	//该订单在商户系统的流水号
		$orderMoney=trim($_GET["orderMoney"])*100;  //商户订单实际金额单位：（元）
		$cardno=trim($_GET["cardno"]);          	//支付类型，为固定值 32
		$mark=trim($_GET['mark']);              	//未启用暂时返回空值
		$sign=trim($_GET['sign']);              	//发送给商户的签名字符串
		$resign=trim($_GET["resign"]);          	//发送给商户的签名字符串
		$des=trim($_GET["des"]);                	//描述订单支付成功或失败的系统备注
		
		 $key='244ca72c106b8eef6c27f2e97bfd93ee';
		//$sign=strtoupper(md5("customerid=".$customerid."&sd51no=".$sd51no."&sdcustomno=".$sdcustomno."&mark=".$mark."&key=".$key));
		//$resign2=strtoupper(md5("sign=".$sign."&customerid=".$customerid."&ordermoney=".$ordermoney."&sd51no=".$sd51no."&state=".$state."&key=".$key));
/* 		if($r_sign!=$sign)
		{
			echo "签名不正确1";
			//记录日志
			exit();
		}
		
		if($resign!=$resign2)
		{
			echo "签名不正确2";
			//记录日志
			exit();
		}	 */	
		if($state=="1")
		{
			$yp_apply_order=M('yp_apply_order');
			$yp_order_record=M('yp_order_record');
			$message=M("","","DB_CONFIG0")->query('select * from t_agent where accounts='.$_SESSION['accounts']);
			//此为固定充值钱数，固定送钻；
			if($_GET['orderMoney']==40)
			{
				$gems2=100+$message[0]['gems'];
			}			
			if($_GET['orderMoney']==100)
			{
				$gems2=300+$message[0]['gems'];
			}			
			if($_GET['orderMoney']==350)
			{
				$gems2=1000+$message[0]['gems'];
			}			
			if($_GET['orderMoney']==1000)
			{
				$gems2=3000+$message[0]['gems'];
			}			
			if($_GET['orderMoney']==3000)
			{
				$gems2=10000+$message[0]['gems'];
			}
			$sql=M("","","DB_CONFIG0")->query('select * from yp_order_record where order_no='.$sdcustomno);
			$sql=$yp_order_record->where('order_no='.$sdcustomno)->find();
			if($sql=="")
			{			
				M('','','DB_CONFIG0')->execute("insert into yp_order_record(order_no,state,gateway_no,fill_money,sign)values('$sdcustomno','$state','$sd51no','$orderMoney','$sign')");
				M("","","DB_CONFIG0")->execute("update t_agent set gems='$gems2' where accounts=".$_SESSION['accounts']);
				M("","","DB_CONFIG0")->execute("update yp_apply_order set order_status=1 where order_no=".$sdcustomno);
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('支付成功');location.href='".U('index/payoff')."'</script>";
			}
			elseif($sql!="")
			{
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('请勿重复刷新');location.href='".U('index/payoff')."'</script>";
			}					
		}
		else if($state=="2")
		{
			if(M('','','DB_CONFIG0')->execute("insert into yp_order_record(order_no,state,gateway_no,fill_money,sign)values('$sdcustomno','$state','$sd51no','$orderMoney','$sign')"))
			{	
				M("","","DB_CONFIG0")->execute("update yp_apply_order set order_status='$state' where order_no=".$sdcustomno);	
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('系统繁忙');location.href='".U('index/payoff')."'</script>";
			}
		}
		else
		{
			if(M('','','DB_CONFIG0')->execute("insert into yp_order_record(sdcustomno,state,sd51no,orderMoney,sign)values('$sdcustomno','$state','$sd51no','$orderMoney','$sign')"))
			{
				M("","","DB_CONFIG0")->execute("update yp_apply_order set order_status='$state' where order_no=".$_GET['sdcustomno']);	
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('订单异常');location.href='".U('index/payoff')."'</script>";
			}
		}
		$this->display();
	}

			
}














